﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-16
/// Purpose: 		用户登录帐户集合类
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2012-6-5        增加通过用户名返回未被禁用的用户帐户对象的只读索引器
/// Robin           2012-7-11       GetLogonUser返回集合中包括已锁定用户
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.RelationalMapping;

namespace RB.Web.Security.Rdb
{
    /// <summary>
    /// 用户登录帐户集合类
    /// </summary>
    public class UserAccountSet : IdKeyedRecordSet<UserAccount, Guid> 
    {
        /// <summary>
        /// 通过用户名获取未被禁用的用户帐户对象
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>用户帐户对象</returns>
        public UserAccount this[string username]
        {
            get
            {
                return this.Where(p => p.UserName == username && !(bool)p.IsDisabled).SingleOrDefault();
            }
        }

        internal UserAccount GetLogonUser(string username)
        {
            var q = this.AsQueryable().Where(p => p.IsApproved && !(bool)p.IsDisabled 
                && p.UserName == username);
            return q.Single();
        }

        internal UserAccount GetLogonUser(string username, string appCode)
        {
            var q = from t in this.AsQueryable()
                    join t1 in (new UserAppEntrySet()).AsQueryable() on t.Id equals t1.UserId
                    where t.IsApproved 
                        && !(bool)t.IsDisabled
                        && t.UserName == username
                        && t1.AppCode == appCode
                    select t;
            return q.Single();
        }

        internal UserAccount GetLogonUser(Guid userId, string appCode)
        {
            var q = from t in this.AsQueryable()
                    join t1 in (new UserAppEntrySet()).AsQueryable() on t.Id equals t1.UserId
                    where t.IsApproved
                        && !(bool)t.IsDisabled
                        && t.Id == userId
                        && t1.AppCode == appCode
                    select t;
            return q.Single();
        }

        /// <summary>
        /// 将某个用户加到一个应用，使其有访问该应用的权限
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="appCode">应用代码</param>
        /// <param name="createBy">创建的用户</param>
        internal void AddUserToApp(Guid userId, string appCode, string createBy)
        {
            UserAppEntrySet set = new UserAppEntrySet();
            set.Add(new UserAppEntry() { UserId = userId, AppCode = appCode, CreateBy = createBy, CreateTime = DateTime.Now });
        }

        public override bool Remove(UserAccount item, DataAccess.Executor exec)
        {
            if (!item.IsBuiltin)
            {
                (new UserAccountRoleSet()).RemoveAll(t => t.UserId == item.Id, exec);
                (new UserAppEntrySet()).RemoveAll(t => t.UserId == item.Id, exec);
                return base.Remove(item, exec);
            }
            return false;
        }
    }
}
